#include "app_message.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "cJson/cJSON.h"

/*

字符数组格式：
buff[0]= conn_type;//连接类型,blue tooth, wifi....
buff[1]= conn_id_len;
buff[2]= conn_message_len;
buff[3+conn_id_len]= conn_id;
buff[3+conn_id_len+conn_message_len]= conn_message;

json格式: {"conn_type": 1, "id": "5858", "msg": "61626364"}

*/

/**
 * @function: 调用这个函数会将下行设备发来的字符信息转换为json字符串
 * @charsBuff: 下游设备发来的字符数组
 * @json_str: 最终的返回结果json字符串
 */
returnType_t app_message_chars2json(char *charsBuff, char *json_str)
{

    /* 0. take message from charsBuff */
    __uint8_t conn_type = charsBuff[0];
    __uint8_t conn_id_len = charsBuff[1];
    __uint8_t conn_message_len = charsBuff[2];

    char id[conn_id_len + 1];
    memcpy(id, charsBuff + 3, conn_id_len);
    id[conn_id_len] = '\0'; /* offset == conn_id_len is last element*/

    char message[conn_message_len + 1];
    memcpy(message, charsBuff + 3 + conn_id_len, conn_message_len);
    message[conn_message_len] = '\0'; /* offset == conn_message_len is last element*/

    /* 1. create json object */
    cJSON *obj = cJSON_CreateObject();
    if (obj == NULL)
    {
        return ERROR;
    }

    /* 2. id/message -> hexStr   */

    char *idHexStr = app_common_str2hex(id);
    char *messageHexStr = app_common_str2hex(message);

    /* 3. add value
     json格式: {"conn_type": 1, "id": "5858", "msg": "61626364"}
    */
    cJSON_AddNumberToObject(obj, "conn_type", conn_type);
    cJSON_AddStringToObject(obj, "id", idHexStr);           /* hexStr */
    cJSON_AddStringToObject(obj, "msg", messageHexStr); /* hexStr */

    /* 4. json obj -> json string*/
    char *json = cJSON_PrintUnformatted(obj);
    memcpy(json_str, json, strlen(json));

    /* 5. free memory*/
    cJSON_Delete(obj);
    free(idHexStr);
    free(messageHexStr);
    free(json);

    return SUCCESS;
}

/**
 * @function 上行设备发来的json字符串转换为字符信息
 * @json_str: 上行设备发来的json字符串
 * @charsBuff: 最终的返回结果字符数组
 * @returnType_t  if success return SUCCESS, else return ERROR
 */
returnType_t app_message_json2chars(char *json_str, char *charsBuff)
{
    /* 1. take message from json_str */
    cJSON *obj = cJSON_Parse(json_str);
    if (obj == NULL)
    {
        return ERROR;
    }
    int conn_type = cJSON_GetObjectItem(obj, "conn_type")->valueint;
    char *idHexStr = cJSON_GetObjectItem(obj, "id")->valuestring;
    // char *messageHexStr = cJSON_GetObjectItem(obj, "message")->valuestring;/*取一个不存在的KEY——VLAUE会导致段错误 */
    char *messageHexStr = cJSON_GetObjectItem(obj, "msg")->valuestring;

    printf("conn_type: %d, id: %s, message: %s\n", conn_type, idHexStr, messageHexStr);

    /* 2. hexStr -> Str */
    char *idStr = app_common_hex2str(idHexStr);
    if (idStr == NULL)
    {
        return ERROR;
    }
    char *mesStr = app_common_hex2str(messageHexStr);
    if (mesStr == NULL)
    {
        return ERROR;
    }

    /* 3. result -> charsBuff */
    charsBuff[0] = conn_type;
    charsBuff[1] = strlen(idStr);
    charsBuff[2] = strlen(mesStr);
    memcpy(charsBuff + 3, idStr, strlen(idStr));
    memcpy(charsBuff + 3 + strlen(idStr), mesStr, strlen(mesStr));

    // /* 4. free memory */
    cJSON_Delete(obj);
    // free(idHexStr); /* 0bj已经释放了，再来就报错 */
    // free(messageHexStr);
    free(idStr);
    free(mesStr);

    return SUCCESS;
}
